﻿using GSPatEditor.Pat;
using GSPatEditor.Pat.Effect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GSPatEditor.Serialization.ProjectSegment
{
    [Serializable]
    class FilteredEffectData
    {
        public FilterBase[] Filters;
        public EffectBase Effect;
    }
    [Serializable]
    class ActionSegment : SegmentBase
    {
        public int Name;

        public FilteredEffectData[] InitEffect;
        public FilteredEffectData[] UpdateEffect;
        public FilteredEffectData[] HitEffect;
        public FilteredEffectData[][] MotionEventEffect;

        public void AddToProject(Pat.PatProject project)
        {
            project.ActionList.Add(Name, CreateAction());
        }
        public NormalAction CreateAction()
        {
            var action = new NormalAction();
            action.InitEffect = InitEffect.ReadFromList();
            action.UpdateEffect = UpdateEffect.ReadFromList();
            action.HitEffect = HitEffect.ReadFromList();
            if (MotionEventEffect != null)
            {
                action.MotionEventEffect = MotionEventEffect.Select(t => t.ReadFromList()).ToArray();
            }
            return action;
        }
        public void ReadFromProject(int name, NormalAction action)
        {
            this.Name = name;
            this.InitEffect = action.InitEffect.ReadFromList();
            this.UpdateEffect = action.UpdateEffect.ReadFromList();
            this.HitEffect = action.HitEffect.ReadFromList();
            if (action.MotionEventEffect == null)
            {
                this.MotionEventEffect = null;
            }
            else
            {
                this.MotionEventEffect = action.MotionEventEffect.Select(t => t.ReadFromList()).ToArray();
            }
        }
    }
    static class ActionReadingExt
    {
        public static FilteredEffectData[] ReadFromList(this List<FilteredEffect> list)
        {
            var ret = from fe in list
                      select new FilteredEffectData
                      {
                          Effect = fe.Effect,
                          Filters = fe.Filter.ToArray(),
                      };
            return ret.ToArray();
        }
        public static List<FilteredEffect> ReadFromList(this FilteredEffectData[] list)
        {
            var ret = from fe in list
                      select new FilteredEffect
                      {
                          Effect = fe.Effect,
                          Filter = fe.Filters.ToList(),
                      };
            return ret.ToList();
        }
    }
}
